class Solution {
public:
    unordered_map<string,vector<pair<string,double>>> g[2];
    double ans = 0;

    double maxAmount(string initialCurrency, vector<vector<string>>& pairs1, vector<double>& rates1, vector<vector<string>>& pairs2, vector<double>& rates2) {
        for(int i = 0;i < pairs1.size();i++)
        {
            string u = pairs1[i][0];
            string v = pairs1[i][1];
            g[0][u].push_back({v,rates1[i]});
            g[0][v].push_back({u,1 / rates1[i]});
        }
        for(int i = 0;i < pairs2.size();i++)
        {
            string u = pairs2[i][0];
            string v = pairs2[i][1];
            g[1][u].push_back({v,rates2[i]});
            g[1][v].push_back({u,1 / rates2[i]});
        }

        auto dfs2 = [&](this auto&& dfs2,string& cur,string& last,double sum)->void{
            if(cur == initialCurrency) ans= max(ans,sum);

            //只能走第二天
            for(auto& e : g[1][cur])
            {
                string next = e.first;
                double rate = e.second;
                if(next == last) continue;
                dfs2(next,cur,sum * rate);
            }
        };

        auto dfs1 = [&](this auto&& dfs1,string& cur,string& last,double sum)->void
        {
            if(cur == initialCurrency) ans = max(ans,sum);

            //在第一天
            for(auto& e : g[0][cur])
            {
                string next = e.first;
                double rate = e.second;
                if(next == last) continue;
                dfs1(next,cur,sum * rate); 
            }

            //在第二天
            for(auto& e : g[1][cur])
            {
                string next = e.first;
                double rate = e.second;
                dfs2(next,cur,sum * rate); 
            }
        };
        string tmp = " ";
        dfs1(initialCurrency,tmp,1);
        return ans;
    }
};